<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace news\admin\controller;
use \news\admin\model\module as modulemodel;
use \temmoku\controller;
use \temmoku\db;
use \temmoku\lib\custom_Model;
use \news\public_class\cache;
use \news\init;
class Module extends Controller{
	
	public function add(){
		!isset($_POST['step']) && $_POST['step']='';
		if('post'===$_POST['step']){
			if(!$_POST['post']['name']){
	    		$this->err('类型名称不可为空');
			}
			(new modulemodel)->add($_POST['post']);
			cache::Module_Cache();
    		$this->Success('成功创建类型',admin_module_url."/module/index");
		}
		$this->display(tpl('module/add'));
	}
	public function index(){
		$data=(new modulemodel)->alllist();
		$this->assign('modelDB', $data);
		$this->display(tpl('module/index'));
	}
	public function state(){
		db::update(init::$table."module")->cols(['close'=>$_POST['state']])->where("id='$_POST[id]'")->query();
		echo json_encode(['Result'=>'1','content'=>'操作成功']);
	}
	public function edit(){
		if($_POST['step']=='name'){
			db::update(init::$table."module")->cols(['name'=>$_POST['value']])->where("id='$_POST[id]'")->query();
		}
		echo json_encode(['Result'=>'1','content'=>'操作成功']);
	}
	//模型排序
	public function reorder(){
		foreach($_POST['reorder'] AS $id=>$reorder){
			db::update(init::$table."module")->cols(['reorder'=>$reorder])->where("id='$id'")->query();
		}
		$this->success('排序成功');
	}
	
	//字段排序
	public function field_reorder(){
		foreach($_POST['reorder'] AS $id=>$reorder){
			db::update(init::$table."field")->cols(['reorder'=>$reorder])->where("id='$id'")->query();
		}
		cache::Module_Cache();
		(new modulemodel)->make_html($_POST['mid']);
		$this->success('排序成功');
	}
	
	public function field_list(){
		$data=db::select('*')->from(init::$table."field")->where("mid = '$_GET[mid]'")->orderByDESC(['reorder'])->query();
		$this->assign('data', $data);
		$this->display(tpl('module/field_list'));
	}
	
	public function del(){
		$data=(new modulemodel)->del($_GET['mid']);
		cache::module_cache();
    	$this->success('类型删除成功');
	}

	//删除字段
	public function del_field(){
		$row=db::select('*')->from(init::$table.'field')->where('id='.$_GET['id'])->row();
		
		db::delete(init::$table.'field')->where('id='.$row['id'])->query();
		db::delete(init::$table.'attribute_list')->where('mid='.$row['id'])->query();
		//查询是否存在字段

		$Describe= db::query(" Describe  `".init::$table."content_".$row['mid']."`  `".$row['field']."` ");
		if($Describe){
			//存在则删除
			db::query("ALTER TABLE `".init::$table."content_".$row['mid']."` DROP `".$row['field']."`");
		}
		cache::module_cache();
		(new modulemodel)->make_html($row['mid']);//更新发布模板
		$this->Success('属性删除成功');
	}
	
	public function field_add(){
		$_data=db::select('*')->from(init::$table."module")->where("id = '$_GET[mid]'")->row();
		if('post'===$_POST['step']){

			if(!$_POST['post']['name']){
				$this->err("属性名称不可为空");
			}
			
			if(strlen($_POST['post']['field'])<3){
				$_POST['post']['field']=(new modulemodel)->field_test($_GET['mid']);
			}
			
			if(is_numeric(substr($_POST['post']['field'],'0','1'))){
				$this->err("属性key开头不要用数字");
			}
			if(!$_POST['post']['type']){
				$this->err("请选择一个属性类型");
			}
			$Describe= db::query(" Describe  `".init::$table."content_".$_GET['mid']."`  `".$_POST['post']['field']."` ");
			if($Describe){
				$this->err("字段".$_POST['post']['field']."已经存在");
			}
			$_POST['post']['is_xss']=intval($_POST['post']['is_xss']);
			$Field_properties=(new Custom_Model)->Field_properties($_POST['post']['type'],$_POST['post']['field_attr'],$_POST['post']['field_length']);
			if($_POST['post']['field_length']){
				$_POST['post']['field_length']=$Field_properties['leng'] ? $Field_properties['leng'] : '';
			}
			$_POST['post']['mid']=$_GET['mid'];
			//创建向模型里插入字段
			db::query("ALTER TABLE `".init::$table."content_".$_GET['mid']."` ADD `".$_POST['post']['field']."` ".$Field_properties['properties']."  NULL $Field_properties[DEFAULT]");
			$_POST['post']['setting']=serialize($_POST['post']['setting']);
			$_POST['post']['reorder']=intval($_POST['post']['reorder']);
			
			$_POST['post']['height']=intval($_POST['post']['height']);
			
			db::insert(init::$table.'field')->cols($_POST['post'])->query();
			cache::Module_Cache();
			(new modulemodel)->make_html($_GET['mid']);
			$this->Success('属性添加成功',admin_module_url.'/module/field_list/mid_'.$_GET['mid']);
		}
		$html=(new Custom_Model)->Custom_Model_Type("Text");
		$this->assign('html', $html);
		$this->assign('data', $_data);
		$this->display(tpl('module/field_add'));
	}
	public function edit_field(){
		$edit=db::select('*')->from(init::$table."field")->where("id = '$_GET[id]'")->row();
		if('post'===$_POST['step']){
			
			if(!$_POST['post']['name']){
				exit("属性名称不可为空");
			}
			if(strlen($_POST['post']['field'])<3){
				exit("属性KEY能小于三个字符");
			}
			if(is_numeric(substr($_POST['post']['field'],'0','1'))){
				exit("属性开头不要用数字");
			}
			if(!$_POST['post']['type']){
				exit("请选择一个属性类型");
			}
			$_POST['post']['is_xss']=intval($_POST['post']['is_xss']);
			
			$Field_properties=(new Custom_Model)->Field_properties($_POST['post']['type'],$_POST['post']['field_attr'],$_POST['post']['field_length']);
			$_POST['post']['field_length']=$Field_properties['leng'] ? $Field_properties['leng'] : '';
			$field=$_POST['post']['field'];
			//状态都是相同的，但是字段名变动时才去更改
			if($edit['field'] != $ziduan_key){
				db::query("ALTER TABLE `".init::$table."content_$edit[mid]` CHANGE `".$edit['field']."` `$field` $Field_properties[properties]  NULL  $Field_properties[DEFAULT]");
			}
			$_POST['post']['setting']=serialize($_POST['post']['setting']);
			$_POST['post']['reorder']=intval($_POST['post']['reorder']);
			$_POST['post']['height']=intval($_POST['post']['height']);
			db::update(init::$table."field")->cols($_POST['post'])->where("id=$_GET[id]")->query();
			cache::Module_Cache();
			(new modulemodel)->make_html($edit['mid']);
			$this->Success('属性修改成功',admin_module_url.'/module/field_list/mid_'.$edit['mid']);
		}
		
		
		$edit['setting']=unserialize($edit['setting']);
		
		$data=db::select('name')->from(init::$table."module")->where("id = '$edit[mid]'")->row();
		//获取字段所属属性
		$html=(new Custom_Model)->Custom_Model_Type($edit['type'],$edit);
		$this->assign('data', $data);
		$this->assign('html', $html);
		$this->assign('edit', $edit);
		$this->display(tpl('module/field_add'));
	}
	
	public function ajax_field(){
		echo (new Custom_Model)->Custom_Model_Type($_GET['type']);
	}

}
?>
